/**
 * @name         RepNET Credit Remittance Controller
 * @version      1.0a
 * @author       Ramindu Deshapriya <rasade88@gmail.com>
 * @author 		 Thusira Dissanayake <thusira14@gmail.com>
 * @about        Developed by MiDi
 * @license	 http://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License (LGPL)
 * @lastModified 2012.09.24
 */
package com.midi.repnet.main;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import java.sql.*;

import com.midi.repnet.models.Shop;

/**
 * Class to handle Credit Remittance data entry
 * 
 * @author Ramindu
 * 
 */
public class CreditRemittance {
	public static boolean submitData(String billno, float cashAmount,
			HashMap<String, String> chequeData) {
		Shop shop = new Shop();
		
		float remainder = 0.0f;
		DecimalFormat df = new DecimalFormat();
        df.setMinimumFractionDigits(2);
        df.setMaximumFractionDigits(2);
        df.setGroupingUsed(false);
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateNow = formatter.format(currentDate.getTime());
		try {
			Statement shopStatement = (Statement)Main.dbConnection.createStatement();
			ResultSet shopResult = shopStatement.executeQuery("SELECT * FROM shops as s LEFT JOIN transactions as t ON t.shop_id=s.id WHERE t.bill_no='"+billno+"'");

			while ( shopResult.next() ) {
				shop.setId(new Integer(shopResult.getString("id")));
				shop.setName(shopResult.getString("name"));
			}
			PreparedStatement remainderStatement = (PreparedStatement)Main.dbConnection.prepareStatement("SELECT * FROM transactions WHERE bill_no=?");
			remainderStatement.setString(1, billno);
			
			ResultSet res = remainderStatement.executeQuery();
			if ( res.next() ) {
				remainder = new Float(res.getString("remainder"));
			}

			if ( chequeData != null ) {
				remainder = remainder - (cashAmount + new Float(chequeData.get("amount")));
				PreparedStatement chequeInsertStatement = (PreparedStatement)Main.dbConnection.prepareStatement(
						"INSERT INTO cheques VALUES(?,?,?,?,?,?,'ok')");
				chequeInsertStatement.setString(1, chequeData.get("cheque_no"));
				chequeInsertStatement.setString(2, billno);
				chequeInsertStatement.setInt(3, shop.getId());
				chequeInsertStatement.setString(4, df.format(new Float(chequeData.get("amount"))));
				chequeInsertStatement.setString(5, chequeData.get("date"));
				chequeInsertStatement.setString(6, chequeData.get("bank"));
				int ChequeSuccess = chequeInsertStatement.executeUpdate();
						
				if ( ChequeSuccess == 0 ) {
					return false;
				}
			}
			else {

				remainder = remainder - cashAmount;
			}
			if ( cashAmount > 0.0f ) {
				Statement cashInsertStatement = (Statement) Main.dbConnection.createStatement();
				int cashSuccess = cashInsertStatement.executeUpdate("INSERT INTO cash_payment(bill_no,shop_id,amount,payment_date) VALUES('"+billno+"','"+shop.getId()+"','"+cashAmount+"','"+dateNow+"')");
				if ( cashSuccess == 0 ) {
					return false;
				}
			}
			PreparedStatement remainderUpdateStatement = (PreparedStatement)Main.dbConnection.prepareStatement("UPDATE transactions SET remainder=? WHERE bill_no=?");
			remainderUpdateStatement.setString(1, df.format(remainder));
			remainderUpdateStatement.setString(2, billno);
			int remainderSuccess = remainderUpdateStatement.executeUpdate();
			if ( remainderSuccess == 0 ) {
				return false;
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

}
